//
//  CCMLoginTextFieldView.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/28.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import UIKit
import SVProgressHUD

class CCMLoginTextFieldView: UIView {

    fileprivate lazy var phoneLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var verifyLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var lineView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.hex(0x666666)
        return view
    }()
    
    lazy var phoneTextField:UITextField = {
        let textField = UITextField()
        textField.textColor = UIColor.white
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.backgroundColor = UIColor.clear
        textField.keyboardType = .numberPad
        textField.delegate = self
        return textField
    }()
    
    lazy var verifyTextField:UITextField = {
        let textField = UITextField()
        textField.textColor = UIColor.white
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.backgroundColor = UIColor.clear
        textField.keyboardType = .numberPad
        textField.delegate = self
        return textField
    }()
    
    fileprivate lazy var verifyButton:UIButton = {
        let button = UIButton()
        button.setTitle("获取验证码", for: .normal)
        button.setTitleColor(UIColor.hex(0xD9232F), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self, action: #selector(getAuthCode(button:)), for: .touchUpInside)
        return button
    }()
    
    fileprivate var codeTimer: Timer?
    
    fileprivate var codeInterval: Int = 60 {
        didSet {
            if codeInterval > 0 {
                verifyButton.setTitleColor(UIColor.hex(0xD9232F), for: .normal)
                verifyButton.setTitle("剩余\(codeInterval)秒", for: .normal)
            } else {
                verifyButton.setTitleColor(UIColor.hex(0xD9232F), for: .normal)
                verifyButton.setTitle("获取验证码", for: .normal)
                codeInterval = 60
                verifyButton.isEnabled = true
                closeTimer()
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
        self.phoneLabel.text = "手机号"
        self.verifyLabel.text = "验证码"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension CCMLoginTextFieldView{
    
    fileprivate func setUI(){
        
        self.addSubview(self.phoneLabel)
        self.phoneLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(18)
            make.leading.equalTo(self).offset(15)
        }
        
        self.addSubview(self.verifyLabel)
        self.verifyLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(self).offset(-18)
            make.leading.equalTo(self).offset(15)
        }
        
        self.addSubview(self.lineView)
        self.lineView.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(15)
            make.centerY.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(0.5)
        }
        
        self.addSubview(self.phoneTextField)
        self.phoneTextField.snp.makeConstraints { (make) in
            make.centerY.equalTo(phoneLabel)
            make.leading.equalTo(self).offset(73)
            make.trailing.equalTo(self)
            make.height.equalTo(30)
        }
        
        self.addSubview(self.verifyTextField)
        self.verifyTextField.snp.makeConstraints { (make) in
            make.centerY.equalTo(verifyLabel)
            make.leading.equalTo(self).offset(73)
            make.trailing.equalTo(self).offset(-100)
            make.height.equalTo(30)
        }
        
        self.addSubview(self.verifyButton)
        self.verifyButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(self).offset(-15)
            make.centerY.equalTo(self.verifyLabel)
        }
    }
    
    fileprivate func closeTimer() {
        codeTimer?.invalidate()
        codeTimer = nil
    }
    
    @objc fileprivate func getAuthCode(button: UIButton) {
        
        if !phoneTextField.text!.isPhoneNumber() {
            SVProgressHUD.showError(withStatus: "请输入正确的手机号")
            return
        }
        
        verifyButton.isEnabled = false
        codeTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown), userInfo: nil, repeats: true)
        RunLoop.current.add(codeTimer!, forMode: RunLoop.Mode.common)
        //接口
        self.phoneTextField.becomeFirstResponder()
    }
    
    @objc fileprivate func countDown() {
        codeInterval -= 1
    }
}


extension CCMLoginTextFieldView :UITextFieldDelegate{
    
    func textField(_ textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool {
        
        
        if phoneTextField == textField
        {
            let text = textField.text!
            let len = text.count + string.count - range.length
            return len<=11
            
        }else{
            
            let text = textField.text!
            let len = text.count + string.count - range.length
            return len<=6
        }
    }
}
